package com.eight.travel.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.travel.dao.StoreMapper;
import com.eight.travel.pojo.Store;
import com.eight.travel.service.StoreService;
import com.eight.travel.utils.BaseResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService{

    @Resource
    StoreMapper storeMapper;

    @Override
    public BaseResp selectByID(Integer sid) {
        Store store = storeMapper.selectById(sid);
        if(store != null){
            return new BaseResp().SUCCESS("查询成功", store,null);
        }
        return new BaseResp().FAIL("查询失败");
    }

    @Override
    public BaseResp selectAll(Integer page, Integer limit) {
        List<Store> stores = storeMapper.selectAll((page-1)*limit,limit);
        int i = storeMapper.selectRow();
        if(stores != null){
            return new BaseResp().SUCCESS("查询成功", stores, i);
        }
        return new BaseResp().FAIL("查询失败");
    }


    @Override
    public BaseResp addStore(Store store) {
        int res = storeMapper.insert(store);
        if(res == 1){
            return new BaseResp().SUCCESS2("添加成功");
        }
        return new BaseResp().FAIL("添加失败");
    }

    @Override
    public BaseResp updateStore(Store store) {
        int res = storeMapper.updateStore(store);
        if(res == 1){
            return new BaseResp().SUCCESS2("修改成功");
        }
        return new BaseResp().FAIL("修改失败");

    }

    @Override
    public BaseResp deleteByID(Integer sid) {
        int res = storeMapper.deleteById(sid);
        if(res == 1){
            return new BaseResp().SUCCESS2("删除成功");
        }
        return new BaseResp().FAIL("删除失败");
    }

    @Override
    public BaseResp updateStorehot(Integer sid) {
        int res = storeMapper.updateStorehot(sid);
        if(res == 1){
            return new BaseResp().SUCCESS2("商场热度+1");
        }
        return new BaseResp().FAIL("未成功");
    }

    @Override
    public BaseResp selectByHot(Integer page, Integer limit) {
        List<Store> stores = storeMapper.selectByHot((page - 1) * limit, limit);
        int i = storeMapper.selectRow();
        if(stores != null){
            return new BaseResp().SUCCESS("热度排序成功", stores, i);
        }
        return new BaseResp().FAIL("热度排序失败");
    }

    @Override
    public BaseResp selectByLike(String storename) {
        List<Store> stores = storeMapper.selectByLike(storename);
        int i = storeMapper.selectByLikeRow(storename);
        if(stores != null){
            return new BaseResp().SUCCESS("模糊查询成功", stores, i);
        }
        return new BaseResp().FAIL("模糊查询失败");
    }
}
